<script setup lang="ts">
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { Search, ShoppingCart, Menu } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

// 导航菜单
const navigation = [
  { name: '首页', href: '/' },
  { name: '商品', href: '/goods' },
  { name: '关于', href: '/about' }
]

// 响应式数据
const searchKeyword = ref('')
const mobileMenuOpen = ref(false)

// 搜索处理
const handleSearch = () => {
  if (searchKeyword.value.trim()) {
    router.push({
      name: 'goods',
      query: { search: searchKeyword.value.trim() }
    })
  }
}
</script>

<template>
  <div
    id="app"
    class="app-container"
  >
    <!-- 顶部导航栏 -->
    <header class="header">
      <nav class="nav-container">
        <!-- Logo -->
        <router-link
          to="/"
          class="logo"
        >
          <div class="logo-icon">商城</div>
        </router-link>

        <!-- 导航菜单 -->
        <div class="nav-menu">
          <router-link
            v-for="item in navigation"
            :key="item.name"
            :to="item.href"
            class="nav-item"
            :class="{ 'nav-active': route.path === item.href }"
          >
            {{ item.name }}
          </router-link>
        </div>

        <!-- 搜索和购物车 -->
        <div class="nav-actions">
          <div class="search-container">
            <el-input
              v-model="searchKeyword"
              placeholder="搜索..."
              class="search-input"
              @keyup.enter="handleSearch"
            >
              <template #prefix>
                <el-icon class="search-icon">
                  <Search />
                </el-icon>
              </template>
            </el-input>
          </div>

          <router-link
            to="/cart"
            class="cart-link"
          >
            <el-icon size="20">
              <ShoppingCart />
            </el-icon>
          </router-link>

          <!-- 移动端菜单按钮 -->
          <button
            class="mobile-menu-btn"
            @click="mobileMenuOpen = !mobileMenuOpen"
          >
            <el-icon size="20">
              <Menu />
            </el-icon>
          </button>
        </div>
      </nav>

      <!-- 移动端导航菜单 -->
      <div
        v-show="mobileMenuOpen"
        class="mobile-menu"
      >
        <router-link
          v-for="item in navigation"
          :key="item.name"
          :to="item.href"
          class="mobile-nav-item"
          @click="mobileMenuOpen = false"
        >
          {{ item.name }}
        </router-link>
      </div>
    </header>

    <!-- 主要内容区域 -->
    <main class="main-content">
      <router-view />
    </main>

    <!-- 页脚 -->
    <footer class="footer">
      <div class="footer-container">
        <div class="footer-content">
          <div class="footer-section">
            <h3>商城</h3>
            <p>专业的电商解决方案</p>
          </div>

          <div class="footer-section">
            <h4>产品</h4>
            <a href="#">商品中心</a>
            <a href="#">购物车</a>
            <a href="#">订单管理</a>
          </div>

          <div class="footer-section">
            <h4>服务</h4>
            <a href="#">客户支持</a>
            <a href="#">配送服务</a>
            <a href="#">退换货</a>
          </div>

          <div class="footer-section">
            <h4>公司</h4>
            <a href="#">关于我们</a>
            <a href="#">联系我们</a>
            <a href="#">隐私政策</a>
          </div>
        </div>

        <div class="footer-bottom">
          <p>© 2025 商城. 保留所有权利.</p>
        </div>
      </div>
    </footer>
  </div>
</template>

<style scoped>
/* 全局重置和字体 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.app-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display',
    'Helvetica Neue', Helvetica, Arial, sans-serif;
  background-color: #fbfbfd;
  color: #1d1d1f;
  line-height: 1.4705882353;
  font-weight: 400;
  letter-spacing: -0.022em;
}

/* 头部导航 */
.header {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.nav-container {
  max-width: 1024px;
  margin: 0 auto;
  padding: 0 22px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 48px;
}

.logo {
  text-decoration: none;
  color: #1d1d1f;
  font-weight: 600;
  font-size: 21px;
  letter-spacing: -0.01em;
}

.logo-icon {
  color: #1d1d1f;
}

.nav-menu {
  display: flex;
  gap: 44px;
  align-items: center;
}

.nav-item {
  text-decoration: none;
  color: #424245;
  font-size: 12px;
  font-weight: 400;
  letter-spacing: -0.01em;
  transition: color 0.3s ease;
  padding: 8px 0;
}

.nav-item:hover {
  color: #1d1d1f;
}

.nav-active {
  color: #1d1d1f;
  font-weight: 600;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 18px;
}

.search-container {
  position: relative;
}

.search-input {
  width: 264px;
  border-radius: 8px;
}

.search-input :deep(.el-input__wrapper) {
  background-color: rgba(0, 0, 0, 0.1);
  border: none;
  border-radius: 8px;
  box-shadow: none;
  padding: 8px 12px;
  transition: all 0.3s ease;
}

.search-input :deep(.el-input__wrapper):hover {
  background-color: rgba(0, 0, 0, 0.15);
}

.search-input :deep(.el-input__wrapper.is-focus) {
  background-color: white;
  box-shadow: 0 0 0 4px rgba(0, 125, 250, 0.6);
}

.search-input :deep(.el-input__inner) {
  color: #1d1d1f;
  font-size: 14px;
  background: transparent;
}

.search-input :deep(.el-input__inner::placeholder) {
  color: #86868b;
}

.search-icon {
  color: #86868b;
}

.cart-link {
  color: #424245;
  text-decoration: none;
  transition: color 0.3s ease;
}

.cart-link:hover {
  color: #1d1d1f;
}

.mobile-menu-btn {
  display: none;
  background: none;
  border: none;
  color: #424245;
  cursor: pointer;
  transition: color 0.3s ease;
}

.mobile-menu-btn:hover {
  color: #1d1d1f;
}

.mobile-menu {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  padding: 20px 22px;
}

.mobile-nav-item {
  display: block;
  text-decoration: none;
  color: #424245;
  font-size: 17px;
  font-weight: 400;
  padding: 12px 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  transition: color 0.3s ease;
}

.mobile-nav-item:last-child {
  border-bottom: none;
}

.mobile-nav-item:hover {
  color: #1d1d1f;
}

/* 主内容区域 */
.main-content {
  flex: 1;
  background-color: #fbfbfd;
}

/* 页脚 */
.footer {
  background-color: #f5f5f7;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  margin-top: auto;
}

.footer-container {
  max-width: 1024px;
  margin: 0 auto;
  padding: 40px 22px 20px;
}

.footer-content {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 1fr;
  gap: 40px;
  margin-bottom: 34px;
}

.footer-section h3 {
  font-size: 19px;
  font-weight: 600;
  color: #1d1d1f;
  margin-bottom: 12px;
}

.footer-section h4 {
  font-size: 12px;
  font-weight: 600;
  color: #1d1d1f;
  margin-bottom: 9px;
  letter-spacing: -0.01em;
}

.footer-section p {
  font-size: 14px;
  color: #86868b;
  line-height: 1.4285714286;
}

.footer-section a {
  display: block;
  font-size: 12px;
  color: #424245;
  text-decoration: none;
  margin-bottom: 9px;
  transition: color 0.3s ease;
}

.footer-section a:hover {
  color: #1d1d1f;
}

.footer-bottom {
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  padding-top: 17px;
}

.footer-bottom p {
  font-size: 12px;
  color: #86868b;
  text-align: center;
}

/* 响应式设计 */
@media (max-width: 1068px) {
  .nav-container {
    padding: 0 20px;
  }

  .footer-container {
    padding: 40px 20px 20px;
  }
}

@media (max-width: 834px) {
  .nav-menu {
    display: none;
  }

  .mobile-menu-btn {
    display: block;
  }

  .search-input {
    width: 200px;
  }

  .footer-content {
    grid-template-columns: 1fr 1fr;
    gap: 30px;
  }
}

@media (max-width: 568px) {
  .nav-container {
    padding: 0 16px;
  }

  .nav-actions {
    gap: 12px;
  }

  .search-input {
    width: 150px;
  }

  .footer-container {
    padding: 30px 16px 15px;
  }

  .footer-content {
    grid-template-columns: 1fr;
    gap: 25px;
  }

  .mobile-menu {
    padding: 20px 16px;
  }
}
</style>

